package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.repository.ITravelCommentRepository;
import cn.wolfcode.luowowo.common.domain.TravelComment;
import cn.wolfcode.luowowo.common.query.TravelCommentObject;
import cn.wolfcode.luowowo.common.service.ITravelCommentService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private ITravelCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Reference
    private ITravelCommentService travelCommentService;


    @Override
    public TravelComment saveOrUpdate(TravelComment travelComment) {
        if (!StringUtils.hasLength(travelComment.getId())) {
            //设置创建时间
            travelComment.setCreateTime(new Date());
        }

        //判断是评论的评论的话
        if (travelComment.getType() == TravelComment.TRAVLE_COMMENT_TYPE) {
            //通过id查询出评论的评论,再设置到当前评论对象里
            Optional<TravelComment> op = repository.findById(travelComment.getRefComment().getId());
            travelComment.setRefComment(op.get());
        }

        repository.save(travelComment);

        return travelComment;
    }

    @Override
    public Page query(TravelCommentObject qo) {
        //查询条件
        Query query = new Query();

        //游记id条件(相当于SQL里的where detailId = #{detailId})
        if (qo.getTravelId() != -1) {
            query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        }

        //查总数
        long count = template.count(query, TravelComment.class);

        if (count == 0) {
            return Page.empty();
        }

        //参数1:当前页  注意从0开始
        //参数2:每页显示条数
        //参数3:排序规则  第一个参数表示倒序  第二个参数表示按哪个字段排序
        PageRequest request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(request);

        List<TravelComment> list = template.find(query, TravelComment.class);

        return new PageImpl(list, request, count);
    }

    @Override
    public List<TravelComment> listALL() {
        Query query = new Query();
        query.limit(10).skip(1);
        query.with(Sort.by(Sort.Direction.DESC, "createTime"));
        List<TravelComment> list = template.find(query, TravelComment.class);
        return list;
    }

}
